package com.ltg.file.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltg.file.entity.FileInfo;
import com.ltg.file.mapper.FileInfoMapper;
import com.ltg.file.service.FileInfoService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.BindException;

/**
 * <p> ClassName: FileInfoServiceImpl </p>
 * <p> Package: com.ltg.file.service.impl </p>
 * <p> Description: </p>
 * <p></p>
 *
 * @Author: LTG
 * @Create: 2023/2/13 - 17:42
 * @Version: v1.0
 */

@Service
public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements FileInfoService {


    @Override
    public FileInfo create(MultipartFile file) {
//        // 检查文件是否为空
//        if (file == null || file.isEmpty()) {
//            throw new RuntimeException("文件不能为空");
//        }
//        long fileSize = file.getSize();
////        String mediaId = idGenerator.nextUUID(null);
////        String domain = StrUtil.removeSuffix(properties.getDomain(), "/");
////        String localPath = properties.getLocalPrefix();
//        String contentType = file.getContentType();
//        String originName = file.getOriginalFilename();
//        String extension = FileNameUtil.getSuffix(originName);
//        if (StrUtil.isBlank(contentType)) {
//            contentType = FileUtil.getMimeType(originName);
//        }
//        String filename = mediaId + "." + extension;
//        try {
//            FileUtil.mkdir(localPath);
//            IoUtil.copy(file.getInputStream(), FileUtil.getOutputStream(localPath + File.separator + filename));
//        } catch (IOException e) {
//            log.error(e.getMessage());
//            throw new RuntimeException("保存文件异常！请稍后再试~");
//        }
//        FileInfo fileInfo = new FileInfo();
//        fileInfo.setOriginName(originName);
//        fileInfo.setContentType(contentType);
//        fileInfo.setExtName(extension);
//        fileInfo.setSize(String.valueOf(fileSize / 1024));
//        fileInfo.setPath(filename);
//        fileInfo.setUrl(domain + "/api/v1/common/files/media/" + mediaId);
//        fileInfo.setDownloadUrl(domain + "/api/v1/common/files/media/" + mediaId + "/download");
//        this.save(fileInfo);
//        FileInfo mediaInfo1 = this.getById(mediaId);
//        return mediaInfo1;
        return null;
    }

    @Override
    public void show(HttpServletRequest request, HttpServletResponse response, String id) {

    }

    @Override
    public void download(HttpServletRequest request, HttpServletResponse response, String id) {

    }

    @Override
    public void downloadStatic(HttpServletRequest request, HttpServletResponse response, String staticKey) {

    }
}
